package com.yonyougov.devops.cruxcisupport.rolemenu;

import com.yonyougov.devops.cruxcisupport.rolemenu.dto.AddMulRoleMenuDto;
import com.yonyougov.devops.cruxcisupport.rolemenu.vo.RoleMenuPageLikeDto;
import com.yonyougov.devops.cruxcisupport.rolemenu.vo.RoleMenuPageLikeVo;
import com.yonyougov.devops.cruxcisupport.util.CruxDevOpsIdGenerator;
import com.yonyougov.devops.fwc.core.dao.BaseDao;
import com.yonyougov.devops.fwc.core.entity.Pagination;
import com.yonyougov.devops.fwc.core.service.impl.BaseServiceImpl;
import org.springframework.stereotype.Service;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

/**
 * t_role_menu服务实现
 */
@Service
public class RoleMenuServiceImpl extends BaseServiceImpl<RoleMenuEntity, BigInteger> implements RoleMenuService {
    private final RoleMenuDao roleMenuDao;

    public RoleMenuServiceImpl(RoleMenuDao roleMenuDao) {
        this.roleMenuDao = roleMenuDao;
    }

    @Override
    public BaseDao<RoleMenuEntity, BigInteger> getDao() {
        return roleMenuDao;
    }

    //<!--用户自定义代码开始-->

    @Override
    public Pagination<RoleMenuPageLikeVo> findRoleMenuPageLike(RoleMenuPageLikeDto roleMenuPageLikeDto) {
        List<RoleMenuPageLikeVo> roleMenuPageLikeVoList = roleMenuDao.findRoleMenuPageLike(roleMenuPageLikeDto);
        int totalCount = roleMenuDao.countByLikeRoleMenu(roleMenuPageLikeDto);

        return new Pagination<>(roleMenuPageLikeDto.getMax(), roleMenuPageLikeDto.getPageNum(), totalCount, roleMenuPageLikeVoList);
    }


    private void addBatch(List<RoleMenuEntity> params) {
        roleMenuDao.addBatch(params);
    }

    @Override
    public void deleteBatchRoleMenuByIds(List<BigInteger> ids) {
        roleMenuDao.deleteBatchRoleMenuByIds(ids);
    }

    @Override
    public void addMulRoleMenu(AddMulRoleMenuDto addMulRoleMenuDto) {
        List<RoleMenuEntity> roleMenuEntities = new ArrayList<>();
        addMulRoleMenuDto.getMenuId().forEach(menuId -> {
            RoleMenuEntity roleMenuEntity = new RoleMenuEntity();
            roleMenuEntity.setId(new CruxDevOpsIdGenerator().generatorId(roleMenuDao));
            roleMenuEntity.setRoleId(addMulRoleMenuDto.getRoleId());
            roleMenuEntity.setMenuId(menuId);
            roleMenuEntities.add(roleMenuEntity);
        });
        addBatch(roleMenuEntities);
    }
    //<!--用户自定义代码结束-->
}
